package real.unknown.math;
import java.util.*;

import org.junit.Test;

/* 
1、给定最长0xff字节的16进制数据，如unsigned char *hex = "0095AFF9F703A8"（最长可能100个字符） ， 要求以10进制格式打印出该数值
*/
public class OxToD {

    public String oxToD(String ox) {
        String sum = "0";
        int len = ox.length();
        for (int i = 0; i < ox.length(); i++) {
            String thisPos = strMul("" + charToNumber(ox.charAt(i)), myPow(i, 16));
            System.out.println(thisPos);
            sum = strAdd(sum, thisPos);
        }
        return sum;
    }

    public String myPow(int x, int n) {
        String temp = x + "", res = "1";
        while (n != 0) {
            if ((n & 1) != 0) res = strMul(res, temp);
            temp = strMul(temp, temp);
            n >>>= 1;
        }
        return res;
    }

    public String strMul(String p, String q) {
        int l1 = p.length(), l2 = q.length();
        int[] nums1 = new int[l1], nums2 = new int[l2], res = new int[l1 + l2];

        for (int i = 0; i < l1; i++) {
            nums1[i] = p.charAt(i) - '0';
        }

        for (int i = 0; i < l2; i++) {
            nums2[i] = q.charAt(i) - '0';
        }
       
        System.out.println(Arrays.toString(nums1) + "   " + Arrays.toString(nums2));
        for (int i = 0; i < l1; i++) {
            for (int j = 0; j < l2; j++) {
                res[i + j] += nums1[i] * nums2[j];
            }
        }
        System.out.println(Arrays.toString(res));

        int len = l1 + l2, sum = 0, flag = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = len - 1; i >= 0; i--) {
            sum = res[i] + flag;
            flag = sum / 10;
            sum %= 10;
            res[i] = sum;
            sb.insert(0, res[i]);
        }
        System.out.println(Arrays.toString(res));      
        return sb.substring(0, len - 1);
    }

    @Test
    public void test1(){
        int n1 = 199992, n2 = 1999992;
        System.out.println(strMul("" + n1, "" + n2));
        System.out.println(n1 * ((long)(n2)));
        Solution s = new Solution();
        System.out.println(s.multiply("" + n1, "" + n2));
    }

    public String strAdd(String p, String q) {
        if (p.length() < q.length()) {
            String temp = p;
            p = q;
            q = temp;
        }

        int l1 = p.length(), l2 = q.length();
        int sum, flag = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q.length(); i++) {
            char c1 = q.charAt(l2 - i - 1);
            char c2 = p.charAt(l1 - i - 1);
            sum = c1 - '0' + c2 - '0' + flag;
            flag = sum / 10;
            sum %= 10;
            sb.insert(0, sum);
        }
        for (int i = l2; i < l1; i++) {
            char c2 = p.charAt(l1 - i - 1);
            sum = c2 - '0' + flag;
            flag = sum / 10;
            sum %= 10;
            sb.insert(0, sum);
        }
           
        if (flag == 1)  sb.insert(0, 1);
        return sb.toString();
    }

    @Test
    public void test() {
      System.out.println(oxToD("0095AFF9F703A8"));
  /*   String s = "0095AFF9F703A8";
    for (int i = 0; i < s.length(); i++) {
        System.out.println(charToNumber(s.charAt(i)));
    } */
    }

    public static void main(String[] args) {
        
    }

    public int charToNumber (char c) {
        int res = c - 'A';
        if (res < 0) return c - '0';
        else return res + 10;
    }

    class Solution {
        
        //定义方法multiply的功能
        public String multiply(String str1,String str2){
            int[] num1 = new int[str1.length()];
            int[] num2 = new int[str2.length()];
            int[] result = new int[str1.length() + str2.length()];

            //将两个字符串转成整型数组，顺序转换，数组下标越小，数字对应的位数越高
            for (int i = 0;i < str1.length(); i++){
                num1[i] = Integer.parseInt(str1.substring(i,i+1));
            }
            for (int i = 0;i < str2.length(); i++){
                num2[i] = Integer.parseInt(str2.substring(i,i+1));
            }

        System.out.println(Arrays.toString(num1) + "   " + Arrays.toString(num2));

            //两大数相乘
            for (int a = 0;a < str1.length(); a++){
                for (int b = 0;b < str2.length(); b++){
                    result[a+b] += num1[a]*num2[b];
                }
            }

            ////判断是否需要进位，满10进1,因为存储顺序与位数高低相反，所以采用逆序进位
            int temp;
            for (int k = result.length-1; k > 0; k--){
                    temp=result[k]/10;  //数组下标大的向数组下标小的进位
                    result[k-1] += temp;
                    result[k] = result[k]%10;
                }

                System.out.println(Arrays.toString(result));
            //将结果数组逆序转化为字符串
            String resultstr = "";
            for (int i = 0; i < result.length-1; i++){
                resultstr += "" + result[i];
            }

            return resultstr;
        }
    }

    @Test
    public void test2() {
        Solution s = new Solution();
        int n1 = 9999, n2 = 99999;
        System.out.println(s.multiply("" + n1, "" + n2));
        long res = (long)n1 * ((long)(n2));
        System.out.println(res);
    }
}